// pages/user/login/login.js
var mta = require('../../../utils/mta_analysis.js');
var app = getApp();
var bg = app.globalData._network_path + 'loginbg.png';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginBg: '../../../images/login_bg.jpg',
    userInfo: '', //获取到的用户信息
    bg: bg
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    mta.Page.init();
  },
  onShow() {
    wx.setStorageSync('is_login', false);
    app.wxLogin();
  },

  //注册
  register() {
    wx.navigateTo({
      url: '../register/register',
    })
  },

  //获取用户手机号码
  getPhoneNumber: function(e) {
    var iv = e.detail.iv,
      encryptedData = e.detail.encryptedData,
      that = this;
    console.log(e)
    if (e.detail.errMsg == 'getPhoneNumber:fail:cancel to confirm login' ||
      e.detail.errMsg == 'getPhoneNumber:fail user deny' || e.detail.rawData == 'undefined') {
      app.showToast('拒绝授权,将无法使用一键登录功能', "none", 2000, function() {});
      return false;
    } else if (e.detail.errMsg == "getPhoneNumber:fail 用户未绑定手机，请先在微信客户端进行绑定后重试") {
      return false;
    } else if (e.detail.errMsg == "getPhoneNumber:fail:cancel to verify sms") {
      return false;
    } else {
      that.getAjaxPhone(iv, encryptedData); //后台解析手机信息
    }
  },

  //后台解析手机号码
  getAjaxPhone(iv, encryptedData) {
    var that = this;
    var openid = wx.getStorageSync('openid');
    app.ajax({
      url: "Common/Wx/aesPhoneDecrypt",
      data: {
        iv: iv,
        data: encryptedData,
        openid: openid
      },
      success: function(res) {
        if (res.data.code == 1000) {
          wx.setStorageSync('phone', res.data.data.phoneNumber);
          app.bindGetnewToken(2, '', 1); //重新调取token

        } else {
          app.showToast(res.data.message, "none", 2000, function() {});
          return false;
        }
      }
    })
  },

})